<?php
/**
 * Created by PhpStorm.
 * Script Name: Sms.php
 * Create: 2022/12/13 14:18
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\common\service;
use Dao\Sms\Sms as SmsClient;

class Sms extends Common
{
    static $instance = null;
    public static string $sign = '【酷云】';

    /**
     * 驱动
     * @param null $id
     * @return array|mixed
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function drivers($id = null){
        $list = [
            'acloud' => '阿里云',
            'qcloud' => '腾讯云',
            'zhutong' => '助通',
            'yunxin' => '云信使',
            'shiyuan' => '示远'
        ];
        return isset($list[$id]) ? $list[$id] : $list;
    }

    /**
     * 发送短信
     * @param $mobile
     * @param $content
     * @return bool|string
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function send($mobile, $content){
        try {
            $content = self::$sign . str_replace(self::$sign, '', $content);
            $sms = self::instance()->send($mobile, $content);
            if($sms !== true){
                return '短信发送错误：' . $sms;
            }
        }catch (\Exception $e){
            return '短信发送错误：' . var_export($e->getMessage(), true);
        }
        return true;
    }

    private static function instance()
    {
        if(is_null(self::$instance)){
            $config = dao_config('system.sms');
            self::$sign = "【{$config['sign']}】";
            self::$instance = new SmsClient($config['account'], $config['pwd'], $config['channel']);
        }
        return self::$instance;
    }
}